import java.util.*;

class Main1 {
    public static void main1(String[] args) {
        int[] arr = {15,30,61,99};
        System.out.println(findMaxGap(arr, 4));
    }
    public static int findMaxGap(int[] a, int n) {
        int[] left = new int[n];
        int[] right = new int[n];
        left[0] = a[0];
        for(int i = 1; i<n; i++) {
            left[i] = Math.max(left[i-1],a[i]);
        }
        right[n-1] = a[n-1];
        for(int i = n-2; i>=0; i--) {
            right[i] = Math.max( right[i+1], a[i]);
        }
        int ret = 0;
        System.out.println(Arrays.toString(left));
        System.out.println(Arrays.toString(right));
        for(int i = 0; i<n-1; i++) {
            ret = Math.max(ret, Math.abs(left[i] - right[i+1]));
        }
        return ret;
    }
}
//2 7 7 7 7
//1 1 1 1 1

class Printer {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
        // write code here
        int[] ret = new int[n*m];
        int index = 0;
        int up = 0, down = n-1, left = 0, right = m-1;

        while (up<=down && left <= right) {
            for(int i = left; i<=right; i++) {
                ret[index++] = mat[up][i];
            }
            up++;

            if(up > down || left > right) {
                break;
            }

            for(int i = up; i<=down; i++) {
                ret[index++] = mat[i][right];
            }
            right--;

            if(up > down || left > right) {
                break;
            }

            for(int i = right; i>=left; i--) {
                ret[index++] = mat[down][i];
            }
            down--;

            if(up > down || left > right) {
                break;
            }

            for(int i = down; i>=up; i--) {
                ret[index++] = mat[i][left];
            }
            left++;
        }

        return ret;
    }
}

class Solution {
    public List<Integer> spiralOrder(int[][] mat) {
        int n = mat.length, m = mat[0].length;
        List<Integer> list = new ArrayList<>();
        int up = 0, down = n-1, left = 0, right = m-1;

        while (up<=down && left <= right) {
            for(int i = left; i<=right; i++) {
                list.add(mat[up][i]);
            }
            up++;

            if(up > down || left > right) {
                break;
            }

            for(int i = up; i<=down; i++) {
                list.add(mat[i][right]);
            }
            right--;

            if(up > down || left > right) {
                break;
            }

            for(int i = right; i>=left; i--) {
                list.add(mat[down][i]);
            }
            down--;

            if(up > down || left > right) {
                break;
            }

            for(int i = down; i>=up; i--) {
                list.add(mat[i][left]);
            }
            left++;
        }

        return list;
    }
}

public class Main {
    public static void main(String[] args) {
        //int[][] arr = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
        int[][] arr = {{1,2}};
        int[] ret = new Printer().clockwisePrint(arr,1,2);
        System.out.println(Arrays.toString(ret));
    }
}
//1  2  3  4
//5  6  7  8
//9  10 11 12
//13 14 15 16

//1 2 3
//4 5 6
//7 8 9